// *******************************************************************************************************
// # サイト共通スクリプト
//   ・トグル開閉
//   ・ページ下部、追従型ボタン
//   ・モーダル
//   ・お気に入りに追加
//   ・スライダーリメイク
//
// *******************************************************************************************************

var APMN = APMN || {};

/**
 * トグル開閉
 * param object
 * - $btn
 * - $target
 * - isOpen 初期状態
 * callback toggle(isOpen) 状態変化時
 * callback moveEnd(isOpen) 状態変化終了時
 */
APMN.ToggleOpener = (function(){
	var that = function(obj){
		this.$btn = obj.btn;
		this.$target = obj.target;
		this.isOpen = obj.isOpen;
		this.isMove = false;
		this.timer = null;
		this._init();
	};
	that.prototype = {
		_init: function(){
			var scope = this;
			this.$btn.on('click', function(e){
				if (scope.isMove) return;
				scope.isMove = true;
				e.preventDefault();
				scope.isOpen = !scope.isOpen;
				if (typeof scope.toggle == 'function') {
					scope.toggle(scope.isOpen);
				};
				scope.$target.stop().slideToggle(400, 'easeOutQuart', function(e){
					clearInterval(scope.timer);
					scope.isMove = false;
					if (typeof scope.moveEnd == 'function') {
						scope.moveEnd(scope.isOpen);
					};
				});
				clearInterval(scope.timer);
				scope.timer = setInterval(function(e){
					if (typeof scope.moving == 'function') {
						scope.moving(scope.isOpen);
					};
				}, 1000/60);
			});
		}
	};
	return that;
}());


/**
 * ページ下部、追従型ボタン
 * param object
 * - isFixed
 * - ieVr
 * fn show()
 * fn hide()
 * fn changeCtsHeight() コンテンツの高さが変わったときにコール
 */
APMN.AppendWrapper = (function(){
	var that = function(obj){
		this._isFixed = obj.isFixed;
		this._ieVr = obj.ieVr;
		this.$wrp = $('#containerWrapper');
		this.$btn = $('#appendContainerWrapper');
		this._isSelected = false;
		this._ctsH = this.$wrp.height();
		this._btnH = this.$btn.height();
		this._ftrH = $('#footerWrapper').height();
		this._winH = $(window).height();
		this._srlT = $(window).scrollTop();
		this._init();
	};
	that.prototype = {
		_init: function(){
			var scope = this;
			if (!this._ieVr) {
				return;
			};
			$(window).on('resize', function(e){
				scope._winH = $(window).height();
				if (scope._isSelected) scope._positionCheck();
			});
			$(window).on('scroll', function(e){
				scope._srlT = $(window).scrollTop();
				if (scope._isSelected) scope._positionCheck();
			});
			setTimeout(function(e){
				scope.changeCtsHeight();
				if (scope._isFixed) {
					scope.show();
				};
			}, 10);
		},
		_positionCheck: function(){
			var _h = this._srlT + this._winH - this._ctsH + this._ftrH;
			if (_h > 0) {//下限界時
				this.$btn.css('bottom', _h);
			} else {//通常時
				this.$btn.css('bottom', 0);
			};
		},
		show: function(){
			if (!this._ieVr || this._isSelected) {
				return;
			};
			this._isSelected = true;
			var _h = this._srlT + this._winH - this._ctsH + this._ftrH;
			if (_h > 0) {//下限界時
				this.$btn.stop(true,true).css({ 'position': 'fixed', 'bottom': _h });
			} else if (_h + this._btnH > 0) {//中途半端時
				this.$btn.stop(true,true).css({ 'position': 'fixed', 'bottom': _h }).animate({
					'bottom': 0
				}, 600, 'easeOutQuart');
			} else {//通常時
				this.$btn.stop().css({ 'position': 'fixed', 'bottom': -this._btnH }).animate({
					'bottom': 0
				}, 600, 'easeOutQuart');
			};
		},
		hide: function(){
			if (this._isFixed || !this._ieVr || !this._isSelected) {
				return;
			};
			this._isSelected = false;
			var scope = this;
			var _h = this._srlT + this._winH - this._ctsH + this._ftrH;
			if (_h > 0) {//下限界時
				this.$btn.stop(true,true).css({ 'position': 'absolute', 'bottom': this._ftrH });
			} else {//通常時, 中途半端時
				this.$btn.stop(true,true).animate({
					'bottom': _h
				}, 400, 'easeInQuart', function(e){
					scope.$btn.css({ 'position': 'absolute', 'bottom': scope._ftrH });
				});
			};
		},
		changeCtsHeight: function(){
			this._ctsH = this.$wrp.height();
			if (this._isSelected) this._positionCheck();
		}
	};
	return that;
}());


/**
 * モーダル
 * param object
 * - $target
 * - $btn
 * - ieVr
 * fn show()
 * fn hide()
 */
APMN.Modal = (function(){
	var that = function(obj){
		this._ieVr = obj.ieVr;
		this.$target = obj.target;
		this.$btn = obj.close;
		this._isAppend = obj.nonAppend;
		this.$bg = '';
		this._bgColor = '#000000';
		this._bgAlpha = 0.7;
		this.init();
	};
	that.prototype = {
		init: function(){
			var scope = this;
			if (!this._isAppend) {
				$('body').append(this.$target);
			};
			this.$btn.on('click', function(e){
				e.preventDefault();
				scope.hide();
			});
		},
		show: function(){
			var scope = this;
			$('body').append('<div id="lean_overlay"></div>');
			this.$bg = $('#lean_overlay');
			this.$bg.fadeTo(200, this._bgAlpha);
			this.$bg.on('click', function(e){
				scope.hide();
			});
			this.$target.fadeTo(200, 1);
			if (!this._ieVr) {
				var _h = $('#containerWrapper').height(),
					_t = $(window).scrollTop() + $(window).height() * 0.5;
				this.$bg.css({
					'position': 'absolute',
					'top': 0,
					'height': _h
				});
				this.$target.css({
					'position': 'absolute',
					'top': _t
				});
				$('#containerWrapper').find('select').hide();
			};
		},
		hide: function(){
			$('#lean_overlay').remove();
			this.$target.stop().hide().css('opacity', 0);
			if (!this._ieVr) {
				$('#containerWrapper').find('select').show();
			};
		},
		alpha:function(num){
			this._bgAlpha = num;
		}
	};
	return that;
}());


/**
 * お気に入りに追加 & この条件を保存
 * param object
 * - path
 * - ieVr
 * fn show()
 * fn hide()
 */
APMN.ModalStar = (function(){
	var that = function(obj){
		this._class = !obj.type ? 'modal_star' : 'modal_save';
		this._ieVr = obj.ieVr;
		this._isShow = false;
		this.$target = '';
		this.$bg = '';
		this._bgColor = '#000000';
		this._bgAlpha = 0.0;
		this._timer = false;
		this._times = 2000;
		this._tp = 0;
		this.init(obj.path);
	};
	that.prototype = {
		init: function(str){
			var _html = '<div id="'+this._class+'">'
					  + '<img src="'+str+'" width="548" height="310" alt="">'
					  + '</div>';
			$('body').append(_html);
			this.$target = $('#'+this._class);
			this._tp = parseInt(this.$target.css('margin-top'));
		},
		show: function(){
			var scope = this;
			$('body').append('<div id="lean_overlay"></div>');
			this.$bg = $('#lean_overlay');
			this.$bg.fadeTo(1, this._bgAlpha);
			this.$bg.on('click', function(e){
				scope.hide();
			});
			this.$target.css({
				'display': 'block',
				'margin-top': this._tp + 50
			}).animate({
				'opacity': 1,
				'margin-top': this._tp
			}, 400, 'easeOutQuart');
			if (!this._ieVr) {
				var _h = $('#containerWrapper').height(),
					_t = $(window).scrollTop() + $(window).height() * 0.5;
				this.$bg.css({
					'position': 'absolute',
					'top': 0,
					'height': _h
				});
				this.$target.css({
					'position': 'absolute',
					'top': _t
				});
				$('#containerWrapper').find('select').hide();
			};
			this._timer = setTimeout(function(e){
				scope.hide();
			}, this._times);
		},
		hide: function(){
			clearTimeout(this._timer);
			$('#lean_overlay').remove();
			this.$target.hide().css('opacity', 0);
			if (!this._ieVr) {
				$('#containerWrapper').find('select').show();
			};
		}
	};
	return that;
}());


/**
 * スライダーリメイク
 * param object
 * - num
 * - target
 * - ieVr
 * return control num
 */
APMN.SliderRemake = function(obj){
	var _num1 = obj.num,
		$target = obj.target,
		_ieVr = obj.ieVr;
		_num2 = $target.find('ul li').length,
		_num3 = 0;

	$target.prepend('<div class="bxslider"></div>');
	for (var i = 0; i < _num2; i++) {
		if (i % _num1 == 0) {
			$target.find('div.bxslider').append('<ul>');
		};
		$target.find('div.bxslider ul').eq(_num3).append($target.find('ul.bxslider li').eq(i).clone());
		if (i % _num1 == _num1 - 1) {
			$target.find('div.bxslider').append('</ul>');
			_num3 ++;
		};
	};
	if (_num2 % _num1 != 0) {
		$target.find('div.bxslider').append('</ul>');
	};
	$target.find('div.bxslider').find('ul').each(function(i){
		// for ie6,7
		if ($(this).find('li').length < 1) {
			$(this).remove();
		};
	});
	$target.find('ul.bxslider').remove();
	return Math.ceil(_num2 / _num1);
};